#!/bin/sh

# Slackware build script for <UrbanTerror>

# Written by (Matthew Fillpot) (mfilpot at gmail.com)

# urbanterror game is utilizing materials that are covered under the following
# licenses:
#  Quake 3 SDK license - the game engine source code is closed source
#  GPL license - ioUrbanTerror uses the GPL license and its source code
#   is openly available
#
# Full copies of the licenses wil lbe installed in /usr/doc/urbanterror-4.1

# TODO LIST:
#   Move binaries from /usr/libexec to /usr/games
#   setup symlinks in /usr/bin to the execs
#
# DATA FILES - /usr/share/games X
# BIN - /usr/games/
# exec - symlinks in /usr/bin

PRGNAM=UrbanTerror
VERSION=${VERSION:-4.1}
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}

CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

SRCVER=$(echo $VERSION|tr -d ".")

if [ "$ARCH" = "i486" ]; then
  EXEC="i386"
elif [ "$ARCH" = "i686" ]; then
  EXEC="i386"
elif [ "$ARCH" = "x86_64" ]; then
  EXEC="x86_64"
fi

set -e # Exit on most errors

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT 
cd $PKG
unzip $CWD/${PRGNAM}_${SRCVER}_FULL.zip -d $PKG

mkdir -p $PKG/usr/share/games/$PRGNAM-$VERSION
mv $PRGNAM $PKG/usr/share/games/$PRGNAM-$VERSION

# Make the current arch execs executable
chmod a+x $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrbanTerror.${EXEC}
chmod a+x $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrTded.${EXEC}

# Move the executables
mkdir -p $PKG/usr/games/$PRGNAM
mv $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrbanTerror.${EXEC} \
   $PKG/usr/games/$PRGNAM/urbanterror
mv $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrTded.${EXEC} \
   $PKG/usr/games/$PRGNAM/ioUrTded.${EXEC}

# Make symlinks to allow the executables to use the data files
cd $PKG/usr/games/$PRGNAM
  ln -s ../../share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrbanTerror.app .
  ln -s ../../share/games/$PRGNAM-$VERSION/$PRGNAM/q3ut4 .
cd -

# Setup symlinks to binaries in /usr/bin
mkdir -p $PKG/usr/bin
cd $PKG/usr/bin
  ln -s ../games/$PRGNAM/urbanterror .
  ln -s ../games/$PRGNAM/ioUrTded.${EXEC} .
cd -

# Place the icon and .desktop files
mkdir $PKG/usr/share/{applications,pixmaps}
cat $CWD/$PRGNAM.desktop > $PKG/usr/share/applications/$PRGNAM.desktop 
cat $CWD/q3ut.png > $PKG/usr/share/pixmaps/q3ut.png

# Remove unused files
rm -f $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/*.exe \
   $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/*.url
if [ $EXEC = i386 ]; then
  rm -f $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/*.x86_64
elif [ $EXEC = x86_64 ]; then
  rm -f $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/*.i386
fi
rm -rf $PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/ioUrbanTerror.app/Contents/MacOS
rm -rf "$PKG/usr/share/games/$PRGNAM-$VERSION/$PRGNAM/q3ut4/Urban Terror on the Web.url"

mkdir -p $PKG/usr/doc/$PRGNAM-$VERSION
mv $PKG/usr/share/games/$PRGNAM-$VERSION/${PRGNAM}/*.txt \
  $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/$PRGNAM.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/$PRGNAM.SlackBuild

mkdir $PKG/install  
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
